﻿using Microsoft.AspNetCore.Mvc.Filters;

namespace Architecture.Api.Filters
{
    /// <summary>
    /// 全局异常过滤器
    /// </summary>
    public class GlobalExceptionFilter : IExceptionFilter
    {
        /// <summary>
        /// 日志
        /// </summary>
        private readonly ILogger<GlobalExceptionFilter> _logger;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="logger">日志对象</param>
        public GlobalExceptionFilter(ILogger<GlobalExceptionFilter> logger)
        {
            _logger = logger;
        }

        /// <summary>
        /// 实现接口
        /// </summary>
        /// <param name="context">异常上下文信息</param>
        public void OnException(ExceptionContext context)
        {
            _logger.LogError("【全局异常过滤器记录错误信息】" + context.Exception.StackTrace);
            context.ExceptionHandled = true;
        }
    }
}
